package com.ys.peony.presenter;

import android.os.SystemClock;
import android.text.TextUtils;

import com.ys.peony.R;
import com.ys.peony.app.BaseApplication;
import com.ys.peony.app.YSConstant;
import com.ys.peony.base.BasePresenter;
import com.ys.peony.base.contract.LoginContract;
import com.ys.peony.http.base.BaseRespBean;
import com.ys.peony.model.LoginModel;
import com.ys.peony.model.UserModel;
import com.ys.peony.ui.activity.LoginActivity;
import com.ys.peony.utils.CommonUtils;
import com.ys.peony.utils.LoadingProgressUtils;
import com.ys.peony.utils.NetUtils;
import com.ys.peony.utils.SharePreferencesUtils;
import com.ys.peony.utils.ToastUtils;

import static com.ys.peony.utils.ToastUtils.showToast;

/**
 * Created by 59153 on 2017/7/26.
 */

public class LoginPresenter extends BasePresenter<LoginModel, LoginActivity> implements LoginContract.ILoginPresenter{

    public LoginPresenter() {
        relateModel(new LoginModel());
    }
    public void login(String userName, String password) {

        if (!CommonUtils.isNotEmpty(userName)) {
            showToast("登录账号不能为空");
            return;
        }
        if (!CommonUtils.isNotEmpty(password)) {
            showToast("登录密码不能为空");
            return;
        }

        if (!NetUtils.isNetworkConnected(BaseApplication.getInstance())) {
            showToast(BaseApplication.getInstance().getResources().getString(R.string.no_net_error));
            return;
        }
        LoadingProgressUtils.showProgressDialog(view);
        model.toLogin(userName,password,this);
    }

    @Override
    public void onSuccess(String respStr) {
        SharePreferencesUtils.getInstance().setKeyValue(YSConstant.USER_INFO, respStr);
        view.gotoMain();
    }

    @Override
    public void onFail(String msg) {
        ToastUtils.showToast(msg);
    }


    /**
     * 描述：判斷當前用戶是否已经登陆过
     * @return
     */
    @Override
    public boolean checkCurUserValid() {
        if(SharePreferencesUtils.getInstance().getUserModel() == null) {
            return false;
        }
        return true;
    }
}
